<?php
defined('IN_IXDCW') or exit('Access Denied');
$do = new AutoSaleModule();
$menus = array (
	array('汽车批发列表', '?moduleid='.$moduleid.'&file=sale'),
);


if(in_array($action, array('', 'check', 'reject', 'recycle'))) {
	$sfields = array( '姓名', '品牌', '生产厂家', '车系', '车款',  '会员名', 'IP');
	$dfields = array( 'truename', 'brand_name', 'make_name', 'car_name', 'model_name', 'username', 'ip');
	$sorder  = array( '添加时间降序', '添加时间升序', '信息ID降序', '信息ID升序');
	$dorder  = array( 'addtime DESC', 'addtime ASC', 'itemid DESC', 'itemid ASC');

	isset($fields) && isset($dfields[$fields]) or $fields = 0;
	isset($order) && isset($dorder[$order]) or $order = 0;
	$level = isset($level) ? intval($level) : 0;

	isset($datetype) && in_array($datetype, array('edittime', 'addtime')) or $datetype = 'addtime';
	$fromtime = $fromdate ? strtotime($fromdate.' 0:0:0') : 0;
	$totime = $todate ? strtotime($todate.' 23:59:59') : 0;

	$itemid or $itemid = '';
	$fields_select = dselect($sfields, 'fields', '', $fields);
	$order_select  = dselect($sorder, 'order', '', $order);
	$condition = 'status=3';
	if($keyword) $condition .= " AND $dfields[$fields] LIKE '%$keyword%'";
	if($psize) $do->setPagesize($psize);
	if($itemid) $condition = " AND itemid=$itemid";
	if($fromtime) $condition .= " AND `$datetype`>=$fromtime";
	if($totime) $condition .= " AND `$datetype`<=$totime";
	$timetype = strpos($dorder[$order], 'edit') === false ? 'add' : '';
}

switch($action) {
	case 'show':
		if(isset($itemid)) {
			$do->setItemid($itemid);
			$r = $do->getOne();
		}
		extract($r);
		$ps = str::stripSlashes($ps);
		include tpl('auto_sale_show', $module);
	break;
	case 'export':
		if($all){
			$lists = $do->getList($condition, $dorder[$order]);
		}else{
			$itemid or msg('请选择客户信息');
			if(is_array($itemid)){
				$itemid = implode(',',$itemid);
				$lists = $db->getAll("select * from {$db->getPre()}auto_sale where itemid in (".$itemid.")");
			}
		}
			$set['author'] = 'ixdcw';
			$set['title']  = '汽车批发客户信息';
			$set['intru'] = '汽车批发客户信息';
			$set['category'] = '汽车批发客户信息';
			$set['stitle'] = '汽车批发客户信息';
			$firstline['A'] = 'itemid|编号|10';
			$firstline['B'] = 'truename|客户姓名|20';
			$firstline['C'] = 'address|申请地址|50';
			$firstline['D'] = 'username|申请会员';
			$firstline['E'] = 'addtime|申请时间';
			$firstline['F'] = 'brand_name|品牌';
			$firstline['G'] = 'make_name|生产厂家';
			$firstline['H'] = 'car_name|车系';
			$firstline['I'] = 'model_name|车款|32';
			$firstline['J'] = 'color|车型颜色';
			$firstline['K'] = 'car_name|车系';
			$firstline['L'] = 'car_name|车系';
			$firstline['M'] = 'mobile|手机号码';
			$firstline['N'] = 'discount|已知优惠';
			$firstline['O'] = 'sale_company|报价单位|30';
			$firstline['P'] = 'ps|其他说明|30';
			foreach($lists as $k=>$t){
				if(OwnCardModule::checkExpire($t['username'])){$lists[$k]['truename'] = $t['truename'].'(讯都车主)';}
				$lists[$k]['addtime'] = date('Y-m-d H:i:s',$t['addtime']);
				$lists[$k]['ps']      = str::stripSlashes($t['ps']);
			}
			$time = date('Y-m-d',$DT_TIME);
			$filename = '汽车批发_'.$time.'.xls';
			Str::exportExcel($lists,$set,$firstline,$filename);
		break;
	case 'delete':
		$itemid or msg('请选择车主信息');
		$do->delete($itemid);
		dmsg('删除成功', $forward);
		break;
	default:
		$lists = $do->getList($condition, $dorder[$order]);
		$menuid = 0;
		include tpl('sale', $module);
	break;
}
?>